//
//  JWaitPayDetailCell.m
//  HeadIndustry
//
//  Created by jun on 2017/9/12.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JWaitPayDetailCell.h"
#import "JPayProductView.h"


@interface JWaitPayDetailCell()

//@property (nonatomic, strong) UIView *buttomView;
@property (nonatomic, strong) YYLabel *statuela;
//@property (nonatomic, assign) NSInteger productNum;
@property (nonatomic, strong) NSMutableArray *cellViewArr;
@property (nonatomic, assign) CGFloat lastMaxY;
@property (nonatomic, strong) UILabel *verdonNameLa;
@property (nonatomic, strong) CALayer *rightImg;
@property (nonatomic, assign) BOOL isFirst;


@end

@implementation JWaitPayDetailCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.opaque = NO;
        _isFirst = NO;
        self.backgroundColor = kRGBColor(245, 245, 245);
        [self configBgCellView];
    }
    return self;
}

- (void)configBgCellView
{
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, self.width, 45);
    topView.opaque = NO;
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
    CALayer *layer = [CALayer layer];
    layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"hgome"].CGImage);
    layer.frame = CGRectMake(15, 11, 20, 20);
    [topView.layer addSublayer:layer];
    
    UILabel *namela = [UILabel new];
    namela.frame = CGRectMake(layer.right + 3, 0, 80, topView.height);
    namela.font = kFONT_TITLE(16);
    [topView addSubview:namela];
    _verdonNameLa = namela;
    
    CALayer *rightImg = [CALayer layer];
    rightImg.contents = (__bridge id _Nullable)([UIImage imageNamed:rightBtnImg].CGImage);
    rightImg.frame = CGRectMake(namela.right + 3, (45 - 16)/2.0, 10, 16);
    [topView.layer addSublayer:rightImg];
    _rightImg = rightImg;
    
    YYLabel *statuLa = [YYLabel new];
    statuLa.frame = CGRectMake(kWindowW - 15 - 100, 0, 100 , 40);
    statuLa.font = kFONT_TITLE(16);
    statuLa.textColor = kRGBColor(150, 150, 150);
    statuLa.textAlignment = NSTextAlignmentRight;
    [topView addSubview:statuLa];
    statuLa.text = [NSString stringWithFormat:@"%@",_waitPayModel.OrderStatus];
    _statuela = statuLa;
}

- (void)configButtomView:(CGFloat)minY
{
    if (_selTypt == 0) {
        [self selzeroType:minY];
    }
    else if (_selTypt == 1) {
        [self seloneType:minY];
    }
    else if (_selTypt == 2 || _selTypt == 4) {
        [self seltwoType:minY];
    }
    
}

- (void)selzeroType:(CGFloat)minY
{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, minY, kWindowW, 10)];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom + 10, kWindowW, 80)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    YYLabel *productFreight = [YYLabel new];
    productFreight.frame = CGRectMake(15 , 0, kWindowW - 15 , 40);
    productFreight.font = kFONT_TITLE(14);
    productFreight.textColor = kRGBColor(150, 150, 150);
    [bgView addSubview:productFreight];
    productFreight.text = [NSString stringWithFormat:@"订单编号: %@",_waitPayModel.OrderNo];
    
    
    YYLabel *productNumLa = [YYLabel new];
    productNumLa.frame = CGRectMake(15, productFreight.bottom, kWindowW - 15 , 40);
    productNumLa.font = kFONT_TITLE(14);
    productNumLa.textColor = kRGBColor(150, 150, 150);
    
    [bgView addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"创建时间: %@",_waitPayModel.CreatedTimeFormat];
}

- (void)seloneType:(CGFloat)minY
{
    
    for (int i = 0; i< _waitPayModel.OrderItemModels.count; i++) {
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = CGRectMake(kWindowW - 15 - 60, 45 + 100 + 5 + i * (50 + 100 + 10), 60, 40);
        [btn1 setTitle:@"退款" forState:UIControlStateNormal];
        [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn1.titleLabel.font = kFONT_TITLE(16);
        btn1.layer.cornerRadius = 20;
        btn1.layer.borderColor = kRGBColor(150, 150, 150).CGColor;
        btn1.layer.borderWidth = 1;
        btn1.tag = i;
        [btn1 addTarget:self action:@selector(payBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn1];
    }
    
    CGFloat maxY =  45 + _waitPayModel.OrderItemModels.count * (100 + 10 + 50) - 10;
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, maxY+ 10, kWindowW, 40)];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
    
    YYLabel *textLa = [YYLabel new];
    textLa.frame = CGRectMake(15 , 0, 70 , 40);
    textLa.font = kFONT_TITLE(15);
    textLa.textColor = kRGBColor(150, 150, 150);
    [topView addSubview:textLa];
    textLa.text = [NSString stringWithFormat:@"实付款"];
    
    YYLabel *textPrice = [YYLabel new];
    textPrice.frame = CGRectMake(textLa.right, textLa.top, kWindowW - 15 - textLa.right, 40);
    textPrice.font = kFONT_TITLE(14);
    textPrice.textColor = [UIColor redColor];
    textPrice.textAlignment = NSTextAlignmentRight;
    [topView addSubview:textPrice];
    textPrice.text = [NSString stringWithFormat:@"%@",_waitPayModel.TotalMoneyText];
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom + 10, kWindowW, 80)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    YYLabel *productFreight = [YYLabel new];
    productFreight.frame = CGRectMake(15 , 0, kWindowW - 15 , 40);
    productFreight.font = kFONT_TITLE(14);
    productFreight.textColor = kRGBColor(150, 150, 150);
    [bgView addSubview:productFreight];
    productFreight.text = [NSString stringWithFormat:@"订单编号: %@",_waitPayModel.OrderNo];
    
    
    YYLabel *productNumLa = [YYLabel new];
    productNumLa.frame = CGRectMake(15, productFreight.bottom, kWindowW - 15 , 40);
    productNumLa.font = kFONT_TITLE(14);
    productNumLa.textColor = kRGBColor(150, 150, 150);
    
    [bgView addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"创建时间: %@",_waitPayModel.CreatedTimeFormat];
}

- (void)seltwoType:(CGFloat)minY
{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, minY, kWindowW, 10)];
    topView.backgroundColor = [UIColor whiteColor];
    [self addSubview:topView];
    
//    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn1.frame = CGRectMake(kWindowW - 15 - 60, topView.bottom + 10, 60, 40);
//    [btn1 setTitle:@"退款" forState:UIControlStateNormal];
//    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    btn1.titleLabel.font = kFONT_TITLE(16);
//    
//    btn1.layer.cornerRadius = 20;
//    btn1.layer.borderColor = kRGBColor(150, 150, 150).CGColor;
//    btn1.layer.borderWidth = 1;
//    btn1.tag = 1;
//    [btn1 addTarget:self action:@selector(payBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:btn1];
    
    YYLabel *textLa = [YYLabel new];
    textLa.frame = CGRectMake(15 , topView.bottom + 10, 70 , 40);
    textLa.font = kFONT_TITLE(15);
    textLa.textColor = kRGBColor(150, 150, 150);
    [self addSubview:textLa];
    textLa.text = [NSString stringWithFormat:@"实付款"];
    
    
    YYLabel *textPrice = [YYLabel new];
    textPrice.frame = CGRectMake(textLa.right, textLa.top, kWindowW - 15 - textLa.right, 40);
    textPrice.font = kFONT_TITLE(14);
    textPrice.textColor = [UIColor redColor];
    textPrice.textAlignment = NSTextAlignmentRight;
    [self addSubview:textPrice];
    textPrice.text = [NSString stringWithFormat:@"%@",_waitPayModel.TotalMoneyText];
    
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, textPrice.bottom + 10, kWindowW, 80)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    YYLabel *productFreight = [YYLabel new];
    productFreight.frame = CGRectMake(15 , 0, kWindowW - 15 , 40);
    productFreight.font = kFONT_TITLE(14);
    productFreight.textColor = kRGBColor(150, 150, 150);
    [bgView addSubview:productFreight];
    productFreight.text = [NSString stringWithFormat:@"订单编号: %@",_waitPayModel.OrderNo];
    
    
    YYLabel *productNumLa = [YYLabel new];
    productNumLa.frame = CGRectMake(15, productFreight.bottom, kWindowW - 15 , 40);
    productNumLa.font = kFONT_TITLE(14);
    productNumLa.textColor = kRGBColor(150, 150, 150);
    
    [bgView addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"创建时间: %@",_waitPayModel.CreatedTimeFormat];
}

- (void)setSelTypt:(NSInteger)selTypt
{
    _selTypt = selTypt;
}

- (void)setWaitPayModel:(JWaitPayModel *)waitPayModel
{
    _waitPayModel = waitPayModel;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        _verdonNameLa.text = _waitPayModel.VendorName;
        _statuela.text = [NSString stringWithFormat:@"%@",_waitPayModel.OrderStatus];
        
        
        [_verdonNameLa sizeToFit];
        CGFloat height = _verdonNameLa.height;
        CGFloat widht = _verdonNameLa.width;
        
        _verdonNameLa.frame = CGRectMake(38, (45 - height)/2.0, widht, _verdonNameLa.height);
        _rightImg.frame = CGRectMake(_verdonNameLa.right + 3, _rightImg.origin.y, _rightImg.width, _rightImg.height);
        
        
        if (_isFirst == NO) {
            
            
            _isFirst = YES;
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                for (int i = 0; i < waitPayModel.OrderItemModels.count; i++) {
                    JPayProductView *cellView = [[JPayProductView alloc]initWithFrame:CGRectMake(0, 45 + i * (100 + 10), kWindowW, 100)];
                    cellView.tag = i;
                    [self.cellViewArr addObject:cellView];
                    
                    if (_selTypt == 1) {
                        cellView.frame = CGRectMake(0, 45 + i * (100 + 10 + 50), kWindowW, 100);
                    }
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self addSubview:cellView];

                        [cellView reloadOrderDataWithModel:waitPayModel.OrderItemModels[i]];
                        
                    });
                    
                }
            });
            
            _lastMaxY = 45 + waitPayModel.OrderItemModels.count * (100 + 10) - 10 ;
            
            [self configButtomView:_lastMaxY];
        }

    });
    
}

- (void)payBtnClick:(UIButton *)btn
{
    if (_block) {
        _block(btn.tag);
    }
}

- (CGFloat)getHeight
{
    CGFloat height = _waitPayModel.OrderItemModels.count * (100 + 10) - 10;
    if (_selTypt == 0) {
        return 45 + height + 10 + 80 + 10 ;
    }
    else if (_selTypt == 1) {
        return height + 10 + 80 + 10 + 40 + 50 ;
    }
    else if (_selTypt == 2 || _selTypt == 4) {
        return height + 10 + 80 + 10 + 50;
    }
    
    return 0;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}



@end
